package com.ruoyi.app.service.impl;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.app.domain.ShopCustomer;
import com.ruoyi.common.utils.CharacterUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MybatisUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.app.mapper.ShopSupplierMapper;
import com.ruoyi.app.domain.ShopSupplier;
import com.ruoyi.app.service.IShopSupplierService;

/**
 * 供应商信息Service业务层处理
 * 
 * @author zhangkaiqiang
 * @date 2024-11-19
 */
@Service
public class ShopSupplierServiceImpl extends ServiceImpl<ShopSupplierMapper, ShopSupplier> implements IShopSupplierService
{
    @Autowired
    private ShopSupplierMapper shopSupplierMapper;

    /**
     * 查询供应商信息
     * 
     * @param id 供应商信息主键
     * @return 供应商信息
     */
    @Override
    public ShopSupplier selectShopSupplierById(String id)
    {
        return baseMapper.selectById(id);
    }

    /**
     * 查询供应商信息列表
     * 
     * @param shopSupplier 供应商信息
     * @return 供应商信息
     */
    @Override
    public List<ShopSupplier> selectShopSupplierList(ShopSupplier shopSupplier)
    {
        LambdaQueryWrapper<ShopSupplier> lambdaQueryWrapper = new LambdaQueryWrapper<>(ShopSupplier.class);
        Map<String, Object> params = shopSupplier.getParams();
        Object beginTime = params.get("beginTime");
        Object endTime = params.get("endTime");
        lambdaQueryWrapper.between((beginTime!= null && endTime!= null), ShopSupplier::getCreateTime, beginTime, endTime);
        lambdaQueryWrapper.eq(ShopSupplier::getShopId, shopSupplier.getShopId());
        lambdaQueryWrapper.orderByAsc(ShopSupplier::getInitial);
        return baseMapper.selectList(lambdaQueryWrapper);
//        return MybatisUtils.list(baseMapper, shopCustomer);
//        return MybatisUtils.list(baseMapper, shopSupplier);
    }


    @Override
    public boolean save(ShopSupplier entity) {
        if(StringUtils.isNotEmpty(entity.getName())){
            entity.setInitial(CharacterUtils.extractInitial(entity.getName()));
        }
        return super.save(entity);
    }


    /**
     * 添加或修改
     * @param shopSupplier
     * @return
     */
    @Override
    public String addOrUpdate(ShopSupplier shopSupplier) {
        if(org.apache.commons.lang3.StringUtils.isEmpty(shopSupplier.getId())){
            save(shopSupplier);
        } else {
            updateById(shopSupplier);
        }
        return shopSupplier.getId();
    }
}
